package com.zcx.qshop.dialog;

import android.content.Context;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;

import com.zcx.helper.bound.BoundView;
import com.zcx.qshop.R;
import com.zcx.qshop.view.LoadBackgoundView;
import com.zcx.qshop.view.LoadLoadingView;
import com.zcx.qshop.view.LoadYCView;

/**
 * Created by Administrator on 2015/8/29 0029.
 */
public class LoadDialog extends QSDialog {

    @BoundView(R.id.load_load) private View load;

    @BoundView(R.id.load_backgound) private LoadBackgoundView backgound;

    @BoundView(R.id.load_yc) private LoadYCView yc;

    @BoundView(R.id.load_loading) private LoadLoadingView loading;

    private AlphaAnimation onAlphaAnimation, offAlphaAnimation, exitAlphaAnimation;

    private boolean isShow;

    public LoadDialog(Context context) {

        super(context, R.style.YCLoadDialog);

        setContentView(R.layout.dialog_load);

        setCancelable(false);

        onAlphaAnimation = new AlphaAnimation(1, 0);

        onAlphaAnimation.setStartOffset(1500);

        onAlphaAnimation.setDuration(1500);

        onAlphaAnimation.setFillAfter(true);

        onAlphaAnimation.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {

                load.setAnimation(offAlphaAnimation);

                offAlphaAnimation.startNow();

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }

        });

        offAlphaAnimation = new AlphaAnimation(0, 1);

        offAlphaAnimation.setDuration(1500);

        offAlphaAnimation.setFillAfter(true);

        offAlphaAnimation.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {

                onAlphaAnimation.setStartOffset(0);

                load.setAnimation(onAlphaAnimation);

                onAlphaAnimation.startNow();

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }

        });

        exitAlphaAnimation = new AlphaAnimation(0.3f, 0);

        exitAlphaAnimation.setDuration(500);

        exitAlphaAnimation.setFillAfter(true);

        exitAlphaAnimation.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {

                dismiss();

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }

        });

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if(keyCode == KeyEvent.KEYCODE_BACK){

            exit();

            return true;

        }

        return super.onKeyDown(keyCode, event);

    }

    @Override
    protected void onStart() {

        super.onStart();

        isShow = true;

        backgound.startNow();

        yc.startNow();

        loading.startNow();

        load.setAnimation(onAlphaAnimation);

        onAlphaAnimation.setStartOffset(3000);

        onAlphaAnimation.startNow();

    }

    @Override
    protected void onStop() {

        backgound.cancel();

        yc.cancel();

        loading.cancel();

        onAlphaAnimation.cancel();

        offAlphaAnimation.cancel();

        exitAlphaAnimation.cancel();

    }

    public void exit() {

        if(isShow){

            isShow = false;

            load.setAnimation(exitAlphaAnimation);

            exitAlphaAnimation.startNow();

        }

    }

}
